//
//  ImportWalletController.m
//  SafPay
//
//  Created by 贺庆博 on 2018/6/19.
//  Copyright © 2018年 ebo. All rights reserved.
//

#import "ImportWalletController.h"
#import "HomeViewController.h"
#import "SetPwdController.h"
#import "XXLinkLabel.h"
#import "NSString+Category.h"
#import "ProperyController.h"
@interface ImportWalletController ()<UITextViewDelegate>
{
    BOOL isAgree;
    UILabel *placeHodleLab;
    UITextView *textView;
}
@property (strong, nonatomic)  NSMutableArray *messageModels;

@end

@implementation ImportWalletController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"导入钱包";
    [self initUI];
    // Do any additional setup after loading the view from its nib.
}
- (void)initUI
{
    textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 90)];

    isAgree = YES;
    textView.font = kFONT(14);
    textView.delegate = self;
    [self.view addSubview:textView];

    placeHodleLab = [UILabel labelWithFrame:CGRectMake(10, 15, kScreenWidth - 20, 30) font:kFONT(14) color:rgb(173,173,173) text:@"助记词（助记词限制12个汉字)"];
    placeHodleLab.userInteractionEnabled = NO;
    [self.view addSubview:placeHodleLab];
    
    XXLinkLabel *protocolLab = [[XXLinkLabel alloc]init];
    
    self.messageModels = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        XXLinkLabelModel *messageModel = [[XXLinkLabelModel alloc]init];
        
        if (i == 0) {
            messageModel.message = @"";
            messageModel.image = kImageName(@"icon_agree");
            messageModel.imageShowSize = CGSizeMake(18, 18);
        }
        if (i == 1) {
            messageModel.message = @"我已经仔细阅读并同意";
            messageModel.extend = @1;
        }
        if (i == 2) {
            messageModel.message = @"服务及隐私条款";
            messageModel.extend = @1;
        }
        [self.messageModels addObject:messageModel];
    }
    protocolLab.textColor = [UIColor blackColor];
    protocolLab.font = kFONT(14);
    __weak typeof(XXLinkLabel*) weakLab = protocolLab;
    protocolLab.imageClickBlock = ^(XXLinkLabelModel *linkInfo) {
        self->isAgree = !self->isAgree;
        linkInfo.image = self->isAgree?kImageName(@"icon_agree"):kImageName(@"icon_unagree");
        weakLab.messageModels = self.messageModels;
    };
    protocolLab.labelClickedBlock = ^(id extend) {
        ProperyController *vc=[ProperyController new];
        [self.navigationController pushViewController:vc animated:YES];
    };
    protocolLab.messageModels = self.messageModels;
    [self.view addSubview:protocolLab];



    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    btn.titleLabel.font = kFONT(16);
    btn.backgroundColor = rgb(33,177,227);
    [self.view addSubview:btn];
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(self->isAgree){
            
            [self importClick];
            
        }else{
            
            [MBProgressHUD showErrorWithText:@"请同意服务及隐私政策"];
        }

    }];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
    [protocolLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(btn.mas_top).mas_offset(-13);
        make.centerX.equalTo(self.view);
    }];
}
-(void)importClick{
    SetPwdController *vc=[SetPwdController new];
    vc.isChange=YES;
    [self.navigationController pushViewController:vc animated:YES];
    return;
    NSString *path=[NSString stringWithFormat:@"%@%@",kBaseUrl,kWalletImport];
    NSMutableDictionary *dict=[NSMutableDictionary new];
    [dict setValue:textView.text forKey:@"mnemonic"];
    __weak UITextView *weakTextView= textView;
    [[TRRequest request] Post:path parameters:dict success:^(TRRequest *request, NSDictionary *responseDict) {
        
        
        MyLog(@"%@",responseDict);
        if([[responseDict valueForKey:@"error"] integerValue]==0){
            
//            [WalletStatusModel saveWalletToken:[[responseDict valueForKey:@"data"]valueForKey:@"remember_token"]];
            Account *account=[[Account alloc]init];
            account.token=[[responseDict valueForKey:@"data"]valueForKey:@"remember_token"];
            account.userId=[[responseDict valueForKey:@"data"]valueForKey:@"uid"];
    
           NSArray *arr= [weakTextView.text getAStringOfChineseWord];
            account.keyWord=[arr componentsJoinedByString:@","];
            [[AccountTools shared]AddUser:account];
            SetPwdController *vc=[SetPwdController new];
            vc.isChange=YES;
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        else{
            
            [MBProgressHUD showErrorWithText:[responseDict valueForKey:@"error_msg"]];
        }
        
    } failure:^(TRRequest *request, NSError *error) {
        
        [MBProgressHUD showErrorWithText:@"网络请求失败"];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    placeHodleLab.hidden = YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    placeHodleLab.hidden = (textView.text.length >0)?YES:NO;
}


@end
